# -*- coding: UTF-8 -*-
from . import db
import json

#software 表的对象
class Software(db.Model):
    __tablename__ = 'software'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    softname = db.Column(db.String(512), unique=True)
    softpath = db.Column(db.String(512))

def software_add(soft_obj):
    db.session.add(soft_obj)
    db.session.commit()

def software_query_id_softname():
    return db.session.query(Software.id, Software.softname).all()

def software_query_from_id(id):
    return db.session.query(Software.softpath).filter(Software.id==id).first()

def software_query_all():
    result = []
    soft_objs = Software.query.all()
    for soft_obj in soft_objs:
        tmp = {}
        tmp['id'] = soft_obj.id
        tmp['softname'] = soft_obj.softname
        tmp['softpath'] = soft_obj.softpath
        result.append(tmp)
    return json.dumps(result)


def software_delete_from_id(id):
    obj = Software.query.filter_by(id = id).first()
    db.session.delete(obj)
    db.session.commit()

def software_update(id, softname, softpath):
    soft_obj = Software.query.filter_by(id=id).first()
    soft_obj.softname = softname
    soft_obj.softpath = softpath
    db.session.commit()